import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBC_select {
    public static void main(String[] args) throws SQLException {
        Scanner sc=new Scanner(System.in);
        //建立并初始化一个数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("root");

        //和数据库服务器建立连接
        Connection connection=dataSource.getConnection();

        //构造sql语句
        //4.查找
        String sql="select * from student";
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            System.out.println("id="+id+",name="+name);
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}




